package com.demo.inner;

import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.apache.commons.lang3.StringUtils;

/**
 * created by lanxinghua@2dfire.com on 2020/2/13
 * 企业微信内部开发   baseService
 */
public class InnerBaseService {
    private static final String corpId = "wwd276de90ff82e1e3";
    private static final String corpSecret = "Mygabj9Vz7q0Z0cxliNVrr0numw_HFyEhU_OWU-8f-c";

    private static WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
    public static WxCpServiceImpl wxCpService;
    public static Integer agentId = 1000004; // 商家应用
    public static String accessToken = "czKjbVG4R0L6LoSPvtKxqTw7gmrAdH0vPSeJAFJrvYE5Lmjt9eYowxoioJadhEUkDIk5u4rinDXBuwIUgbqUXXnqV-3MM1NYmub78RLxCqRMnw_v0Tw0bjByjQpcepvbf5tnRA-ezS6SrFVn-xs_lKJGllFIxs9R7cDXAW5B7Tb5OTffGcBB-wp0_O98sUyS1aC9yoJXlbXF6CInch0uCw";

    // 注册的企业信息
    static {
        config.setCorpId(corpId);
        config.setCorpSecret(corpSecret);
        wxCpService = new WxCpServiceImpl();
        wxCpService.setWxCpConfigStorage(config);
    }

    public static void main(String[] args) throws Exception{
        //System.out.println(getAccessToken(null, null));

    }

    /**
     * 获取accessToken
     * @param corpId
     * @param corpSecret
     * @return
     * @throws Exception
     */
    public static String getAccessToken(String corpId, String corpSecret) {
        try {
            // 后面这里可以设置缓存
            if (StringUtils.isNotBlank(accessToken)){
                return accessToken;
            }

            if (StringUtils.isNoneBlank(corpId, corpSecret)){
                config.setCorpId(corpId);
                config.setCorpSecret(corpSecret);
                wxCpService = new WxCpServiceImpl();
                wxCpService.setWxCpConfigStorage(config);
            }
            return wxCpService.getAccessToken();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
